package main

import (
	"net/http"
	"fmt"
)

var userMap map[string]string = make(map[string]string)

func main()  {

	mux := http.NewServeMux()
	mux.HandleFunc("/login", login)
	mux.HandleFunc("/register", register)

	fmt.Println("111111111111")
	http.ListenAndServe(":3000", mux)
	fmt.Println("22222222222222")
}


func login(responseWriter http.ResponseWriter, request *http.Request)  {

	usernam := request.FormValue("usernam")
	password := request.FormValue("password")

	msg := ""
	if isUserHeFa(usernam, password) && exstistUser(usernam, password) {
		msg = "登录成功"
		fmt.Println(msg, ",usernam:", usernam, ",password:", password)

	} else {
		msg = "登录失败"
		fmt.Println(msg, ",usernam:", usernam, ",password:", password)
	}

	responseWriter.Write([]byte(msg))

}

func register(responseWriter http.ResponseWriter, request *http.Request)  {
	usernam := request.FormValue("usernam")
	password := request.FormValue("password")

	msg := ""
	if isUserHeFa(usernam, password) {
		userMap[usernam] = password
		msg = fmt.Sprint("注册成功,usernam:", usernam, ",password:", password)
		fmt.Println(msg)

	} else {
		msg = fmt.Sprint("注册失败,usernam:", usernam, ",password:", password)
		fmt.Println(msg)
	}

	responseWriter.Write([]byte(msg))
}

func isUserHeFa(username, passwd string) bool {
	if len(username) >= 4 && len(username) <= 10 && len(passwd) >= 4 && len(passwd) <= 10 {
		return true
	} else {
		return false
	}
}

//用户是否存在
func exstistUser(username, passwd string) bool {
	_, ok := userMap[username]
	return ok
}

func nameAndPwdCorrect(username, passwd string)  {
	_, ok := userMap[username]
}


/*
- usernam 长度为4-10
- password 长度为6-16

 */